﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

public partial class System_AuthRole : PageBase
{
    private int rid=-1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.QueryString.ToString()))
        {
           int.TryParse(Request.QueryString.ToString(), out rid);
        }
        if (!IsPostBack)
        {
            if (rid != -1)
            {
                USER_GROUPS group = new GroupManager().Load(rid);
                cbxValid.Checked = group.ACTIVE;
                txtGroupName.Text = group.CODE;

            }
            TreeBindModule();
            TreeView1.Attributes.Add("onclick", "SelectTreeViewNodes()"); 
            
        }
    }

    

    #region 树绑定模块
    protected void TreeBindModule()
    {
        RoleManager rm=new RoleManager();
        ObjectTable table = rm.LoadModules();
        TreeNode pnode = new TreeNode("角色权限列表","-1");
        pnode.Expanded = true;
        foreach (ObjectRow o in table)
        {
            MODULES module = (MODULES)o;
            TreeNode node=new TreeNode(module.NAME,"-1");
            
            pnode.ChildNodes.Add(node);
            ObjectTable auths = rm.LoadAuth(rid);
            if (module != null)
            {

                //获取当前模块下的所有页面
                ObjectTable items = new RoleManager().LoadItems(module.ID);
                int count = 0;
                foreach (ObjectRow row in items)
                {
                    MODULE_ITEMS item = (MODULE_ITEMS)row;
                    
                    TreeNode chilenode = new TreeNode(item.NAME, item.ID.ToString());
                    //遍历是否选中
                    foreach (ObjectRow row2 in auths)
                    {
                        SYS_ROLE_RESOURCES auth = (SYS_ROLE_RESOURCES)row2;
                        if (auth.MODULE_ITEM_ID == item.ID)
                        {
                            chilenode.Checked=true;
                            count++;
                            break;
                        }
                    }
                    if (count ==items.Count)
                        node.Checked = true;
                    node.ChildNodes.Add(chilenode);
                }
            }
            
           
           
        }
        TreeView1.Nodes.Add(pnode);
    }
    #endregion


    #region 保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            StringBuilder sb = new StringBuilder();
            //遍历
            foreach (TreeNode n in TreeView1.Nodes[0].ChildNodes)
            {
                foreach (TreeNode node in n.ChildNodes)
                {

                    //设置权限
                    if (node.Value != "-1" && node.Checked)
                    {
                        sb.AppendFormat("{0},", node.Value);
                    }

                }
            }
            sb.AppendFormat("{0}", -1);
            if (!new GroupManager().Save(rid, txtGroupName.Text.Trim().Replace("'", ""), MyStateManager.DomainID,
                cbxValid.Checked, sb.ToString(), MyStateManager.UserID))
            {
                ShowMessage("保存出错!");
                return;
            }
            else
            {
                ShowMessage("保存成功!", "User_Group_List.aspx");
            }
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                this.MyStateManager.UserID, "角色授权保存 - 异常:" + ex.Message);
            ShowMessage(ex.Message);
        }
       
    }
    #endregion
   
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
          TreeNode selectnode = TreeView1.SelectedNode;
          if (selectnode.ChildNodes.Count > 0)
          {
              TreeView1.CollapseAll();
              TreeView1.Nodes[0].Expand();

              selectnode.Expand();
          }
          else
          {
              if (selectnode.Checked)
                  selectnode.Checked = false;
              else
                  selectnode.Checked = true;
          }

    }




   
}
